package com.nidu.demo.role.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.role.ability.RoleDomainService;
import com.nidu.demo.role.convertor.RoleConvertor;
import com.nidu.demo.role.dto.RoleCO;
import com.nidu.demo.role.dto.RoleUpdateCmd;
import com.nidu.demo.role.gateway.RoleGateway;
import com.nidu.demo.role.model.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 角色更新命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class RoleUpdateCmdExe {

    private final RoleGateway gateway;

    private final RoleDomainService domainService;

    private final RoleConvertor convertor;

    public SingleResponse<RoleCO> execute(RoleUpdateCmd cmd) {
        // 1. 转换为领域对象
        Role role = Role.update(cmd.getId(), cmd.getRoleName(), cmd.getRoleCode(), cmd.getRoleType(), cmd.getDataScope(), cmd.getDataScopeDeptIds(), cmd.getRemark(), cmd.getSortOrder());

        //2. 检验对象是否存在
        domainService.validateDuplicate(role.getRoleCode(), role.getId());

        // 3. 调用网关保存
        Role save = gateway.update(role);

        // 4. 转换为DTO返回
        RoleCO roleCO = convertor.toClientObject(save);

        return SingleResponse.of(roleCO);
    }

    public Response updateStatus(Long id, StatusEnum status) {
        gateway.updateStatus(id, status.getValue());
        return Response.buildSuccess();
    }
} 